

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.thingle.Page;
import org.newdawn.slick.thingle.Theme;


public class TowerOfGods extends BasicGame {

	public static final int GAME=1, MENU=2;

	private int bestScore=-1;
	private int state=MENU;
	private Game game;
	private GamePlay gamePlay;
	private GameLauncher gameLauncher = new GameLauncher(this);
	private boolean f2Pressed = false;

	Player players[] = new Player[]{
			new Player(Color.red,Input.KEY_LEFT,Input.KEY_RIGHT),
	};

	public TowerOfGods() {
		super("TowerOfGods");
		
	}

	@Override
	public void init(GameContainer container) throws SlickException 
	{
	}


	public void newGame()
	{
		game = new Game(gamePlay,1);
		state = GAME;
	}

	@Override
	public void update(GameContainer container, int delta) throws SlickException 
	{
		if(container.getInput().isKeyDown(Input.KEY_F2))
		{
			if(!f2Pressed)
			{
				newGame();
				f2Pressed=true;
			}
		}
		else
			f2Pressed = false;

		if(state==GAME)
		{
			for(int i=0; i<game.getBall().length;i++)
			{
				if(container.getInput().isKeyDown(players[i].getLeftKey()))
				{
					game.getBall()[i].move(Ball.LEFT);
				}
				else if(container.getInput().isKeyDown(players[i].getRightKey()))
				{
					game.getBall()[i].move(Ball.RIGHT);
				}
				else
				{
					game.getBall()[i].move(Ball.STAY);
				}
			}
			game.update();
			if(game.isOver())
			{
				state = MENU;
			}
		}
		
	}



	public void render(GameContainer container, Graphics g) throws SlickException 
	{
		g.setBackground(new Color(200,200,255));

		if(state==GAME)
		{
			Ball[] balls = game.getBall();
			for(int i=0; i<balls.length;i++)
			{
				g.setColor(players[i].getColor());
				g.fillOval((float) balls[i].getX(),(float) (balls[i].getY() + ((Game.HEIGHT/2-balls[i].getY())*2-balls[i].getHeight())),(float) balls[i].getWidth(),(float) balls[i].getHeight());
			}
			g.setColor(Color.blue);
			for(Floor f : game.getFloors())
			{
				float blockWidth = (float)Game.WIDTH / f.getGaps().length;

				for(int i=0; i<f.getGaps().length;i++)
				{	
					if(! f.getGaps()[i])
						g.fillRect(i*blockWidth, (float) (f.getY() + ((Game.HEIGHT/2-f.getY())*2-Floor.height)) ,blockWidth,f.getHeight());
				}

			}
			g.drawString("score: "+game.getFloorsPassed(), 10,25);
			g.drawString("level: "+game.getLevel(), 10,45);
			

		}
		else
		{
			g.setColor(Color.blue);
			g.drawString("Press F2 to begin a new game", 10, 100);
			if(game!=null)
			{
				g.drawString("Last score : "+game.getFloorsPassed(), 10,120);
				if(game.getFloorsPassed()>bestScore)
					bestScore = game.getFloorsPassed();
			}
			if(bestScore>=0)
				g.drawString("Best score : "+bestScore, 10,140);
			g.drawString("<-  et  ->  pour bouger la balle", 10, 270);
			/*g.drawString("w et x pour la balle orange", 10, 250);
			g.drawString("a et r pour la balle rouge", 10, 230);*/
			g.drawString("Bonne chance",10,300);
			g.setColor(Color.red);
			g.drawString("Evitez pendant le plus longtemps que la balle n'atteigne ", 5, 200);
			g.drawString("le dessus de l'écran", 5, 220);
			
		}
	}

	public static void main(String[] args) 
	{ 
		try {
			TowerOfGods t = new TowerOfGods();
			t.setGamePlay(new GamePlay());
			AppGameContainer app = new AppGameContainer(t,t.getGamePlay().getWidth(),t.getGamePlay().getHeight(),false);
			app.setShowFPS(true);

			//app.setMinimumLogicUpdateInterval(1);
			app.setMaximumLogicUpdateInterval(1);
			app.setTargetFrameRate(500);
			app.start(); 
		} catch (SlickException e) { e.printStackTrace(); } 
	}

	public GamePlay getGamePlay() {
		return gamePlay;
	}

	public void setGamePlay(GamePlay gamePlay) {
		this.gamePlay = gamePlay;
	}


}
